<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal"
			aria-hidden="true">×</button>
		<h4 class="modal-title" id="title">商家入驻</h4>
	</div>
	<div class="modal-body">
		<form class="form-horizontal" role="form">
			<div class="form-group">
				<input type="hidden" name="sid" id="sid" value="${data.data.id}">
				<label for="shopName" class="col-sm-4 control-label"> 商家名称 </label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="shopName"
						value="${data.data.shopName}" placeholder="请输入商家名称" required>
				</div>
			</div>
			<div class="form-group">
				<label for="shopLocation" class="col-sm-4 control-label">
					商家地址 </label>
				<div class="col-sm-6">
					<input type="text" class="form-control" id="shopLocation"
						value="${data.data.shopLocation}" placeholder="请输入商家地址" required>
				</div>
			</div>
			<div class="form-group">
				<label for="golder" class="col-sm-4 control-label"> 是否金牌商家 </label>
				<div class="col-sm-6">
					<select class="form-control" id="golden">
						<!--注意选项的匹配-->
						<option value="1">是</option>
						<option value="0"
							<c:if test="${data.data.golden==0}">selected</c:if>>否</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<div class="col-sm-4"></div>
				<div class="col-sm-8">
					<button id="sellerSubmitBtn" type="button"
						class="btn btn-primary col-sm-8">提交</button>
				</div>
			</div>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {
		//获取页面对象
		var sid = $("#sid");//类型：input，隐藏的sid 
		var shopName = $("#shopName");//类型：input，shopName 
		var shopLocation = $("#shopLocation");//类型：input，shopLocation 
		var golden = $("#golden");//类型：select|option  golden 
		var title = $("#title");//类型：h4文本  模态框标题
		//根据Id的情况，智能提示是变更还是入驻
		if (sid.val() != 0) {
			//shopName.attr("disabled", true);
			title.html("商家变更");
		} else {
			// shopName.attr("disabled", false);
			title.html("商家入驻");
		}

		$("#sellerSubmitBtn").click(function() {
			//根据Id的情况，智能提示是变更还是入驻
			if (sid.val() == 0) {
				doAdd();//商家入驻
			} else {
				doUpdate();//商家变更
			}
		});

		function doAdd() {
			//在这里加入所有非空项检测
			$.ajax({
				type : 'POST',
				url : "seller",
				data : {
					method : "add",
					shopName : shopName.val(),
					shopLocation : shopLocation.val(),
					golden : golden.val()
				},
				dataType : 'json',
				success : function(data) {
					if (data.flag == "yes") {
						alert("操作成功");
						location.href = "seller?method=show";
					} else {
						alert("操作失败");
					}
				},
				error : function() {
					alert("操作异常");
				}

			});
		}

		function doUpdate() {
			//在这里加入所有非空项检测
			$.ajax({
				type : 'POST',
				url : "seller",
				data : {
					method : "update",
					sid : sid.val(),
					shopName : shopName.val(),
					shopLocation : shopLocation.val(),
					golden : golden.val()
				},
				dataType : 'json',
				success : function(data) {
					if (data.flag == "yes") {
						alert("操作成功");
						location.href = "seller?method=show";
					} else {
						alert("操作失败");
					}
				},
				error : function() {
					alert("操作异常");
				}

			});
		}

	});
</script>
